import { Component, OnInit, SecurityContext } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { MessageService } from 'src/app/service/message.service';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { User } from 'src/app/model/user';
import { Trainings } from 'src/app/model/trainings';
import { TrainingsDetail } from 'src/app/model/trainingsdetail';

@Component({
  selector: 'app-notifications',
  templateUrl: './notifications.component.html',
  styleUrls: ['./notifications.component.css']
})
export class NotificationsComponent implements OnInit {
  headers = new HttpHeaders({ 'Content-Type': 'application/json' });
  user: User
  response: any
  list_trainingDetail: TrainingsDetail[] = []
  list_trainings: Trainings[] = []

  alerts: any = [];
  constructor(sanitizer: DomSanitizer,
    private http: HttpClient,
    public messageService: MessageService) {
    this.user = JSON.parse(localStorage.getItem('userinfo'))
    this.alerts = this.alerts.map((alert: any) => ({
      type: alert.type,
      msg: sanitizer.sanitize(SecurityContext.HTML, alert.msg)
    }));

    if (this.user.type == 3) {
      this.http.get('/api/trainings/findByMentorId/' + this.user.id + '/0')
        .subscribe((data: any) => {
          console.log('/api/trainings/findByMentorId', data)
          this.response = data;
          this.response.forEach(element => {
            this.list_trainingDetail.push(element)
            this.alerts = []
            this.list_trainingDetail.forEach(item => {
              let type = 'success'
              let msg = '<strong>Proposal! </strong> A user named with \"<strong>' + item.user.name + '</strong>\" has proposed a \"' +
                item.trainings.name + '\" to you. Message: \"' +
                item.trainings.proposal + '\". --- Date: ' + item.trainings.createDate + '.'
              item.trainings.name + '\".'
              this.alerts.push({
                type: type,
                msg: msg
              })
            })
          })
          console.log('this.list_trainingDetail', this.list_trainingDetail)
        })
    } else {
      this.http.get('/api/trainings/findByUserId/' + this.user.id + '/0')
        .subscribe((data: any) => {
          console.log('/api/trainings/findByUserId', data)
          this.response = data;
          this.response.forEach(element => {
            this.list_trainingDetail.push(element)
            this.alerts = []
            this.list_trainingDetail.forEach(item => {
              let type = 'success'
              let msg = '<strong>Proposal! </strong>' + 'You have proposed a \"' +
                item.trainings.name + '\" to a mentor named with \"<strong> ' + item.mentor.name + ' </strong>\". Message: \"' +
                item.trainings.proposal + '\". --- Date: ' + item.trainings.createDate + '.'
              this.alerts.push({
                type: type,
                msg: msg
              })
            })
          })
          console.log('this.list_trainingDetail', this.list_trainingDetail)
        })
    }
  }

  ngOnInit(): void {
  }

}
